<template>
	<view class="u-wrap">
		<view class="u-p-x-30 u-p-y-20 main-bg-color">
			<u-search :showAction="false" disabled @click="tosearchPage"></u-search>
		</view>
		<view class="u-menu-wrap">
			<scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop">
				<view v-for="(item,index) in categoryList" :key="index" class="u-tab-item" :class="[current==index ? 'u-tab-item-active' : '']"
				 :data-current="index" @tap.stop="swichMenu(index)">
					<text class="u-line-1">{{item.name}}</text>
				</view>
			</scroll-view>
			<block v-for="(item,index) in categoryList" :key="index">
				<scroll-view scroll-y class="bg-white" v-if="current==index">
					<view class="flex flex-column align-center u-p-t-10">
						<u-image v-if="categoryList[current]" :src="categoryList[current].bannerimage" width="550rpx" height="175rpx"></u-image>
						<view class="flex flex-column" v-if="categoryList[current]">
						  <view class="flex align-center justify-center">
						    <view class="bg-dark" style="width: 50rpx;height: 5rpx;"></view>
						    <text class="u-p-30 text-dark">{{categoryList[current].name}}</text>
						    <view class="bg-dark" style="width: 50rpx;height: 5rpx;"></view>
						  </view>
						  <view class="flex flex-wrap" style="width: 540rpx;" v-if="categoryList[current].childlist.length">
						    <block v-for="(item1,index1) in categoryList[current].childlist" :key="index1">
						      <view class="flex flex-column align-center item-flex u-m-b-30" style="width: 180rpx;" @click="toPage(item1.id)">
						        <image :src="item1.ImageFrist" style="width: 132rpx; height: 132rpx;" mode="aspectFill"></image>
						        <text class="u-p-t-10">{{item1.name}}</text>
						      </view>
						    </block>
						  </view>
							<view class="" v-else>
								<mescroll-empty :option="{icon: '/static/noData/no-category.png', }"></mescroll-empty>
							</view>
						</view>
					</view>
				</scroll-view>
			</block>
		</view>
    	<u-tabbar v-model="current" :list="list" @change="tabbarChange" active-color="#fe582b" inactive-color="#999999"></u-tabbar>

	</view>
</template>

<script>
	import { CategoryCenterApi } from '@/api/serves.js'
	 import {licenseSubscription} from "@/common/utils/utils"
	export default {
		data() {
			return {
				list: [{
						iconPath: "/static/tabbar/index.png",
						selectedIconPath: "/static/tabbar/index-active.png",
						text: '首页',
           			 	pagePath: "/pages/tabbar/index/index"
					},
					{
						iconPath: "/static/tabbar/category.png",
						selectedIconPath: "/static/tabbar/category-active.png",
						text: '分类',
            			pagePath: "/pages/tabbar/category/category"
					},
          			{
						iconPath: "/static/tabbar/supplier.png",
						selectedIconPath: "/static/tabbar/supplier-active.png",
						text: '商圈',
            			pagePath: "/pages/tabbar/supplier/supplier"
					},
          			{
						iconPath: "/static/tabbar/cart.png",
						selectedIconPath: "/static/tabbar/cart-active.png",
						text: '购物车',
            			pagePath: "/pages/tabbar/cart/cart"
					},
          			{
						iconPath: "/static/tabbar/mine.png",
						selectedIconPath: "/static/tabbar/mine-active.png",
						text: '我的',
            			pagePath: "/pages/tabbar/mine/mine"
					},
				],
				current: 1,
				// 分类列表
				categoryList: [],
				scrollTop: 0, //tab标题的滚动条位置
				current: 0, // 预设当前项的值
				menuHeight: 0, // 左边菜单的高度
				menuItemHeight: 0, // 左边菜单item的高度
			}
		},
		onLoad() {
		  this._initData()
		},
		methods: {
			tabbarChange(e){
				if(e==this.current) return
				if(e == 4){
					licenseSubscription() // 微信小程序授权订阅消息弹窗 
				}
				uni.redirectTo({
				url: this.list[e].pagePath
				})
			},
			_initData() {
			  // 获取分类
			  this.getCategoryList()
			},
			// 获取分类
			getCategoryList() {
			  CategoryCenterApi.getShowlist().then(res => {
			    console.log('获取分类', res);
			    if (res.code == 1) {
			      this.categoryList = res.data.categorydata
			    }
			  })
			},
			// 点击左边的栏目切换
			async swichMenu(index) {
				if(index == this.current) return ;
				this.current = index;
				// 如果为0，意味着尚未初始化
				if(this.menuHeight == 0 || this.menuItemHeight == 0) {
					await this.getElRect('menu-scroll-view', 'menuHeight');
					await this.getElRect('u-tab-item', 'menuItemHeight');
				}
				// 将菜单菜单活动item垂直居中
				this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
			},
			// 获取一个目标元素的高度
			getElRect(elClass, dataVal) {
				new Promise((resolve, reject) => {
					const query = uni.createSelectorQuery().in(this);
					query.select('.' + elClass).fields({size: true},res => {
						// 如果节点尚未生成，res值为null，循环调用执行
						if(!res) {
							setTimeout(() => {
								this.getElRect(elClass);
							}, 10);
							return ;
						}
						this[dataVal] = res.height;
					}).exec();
				})
			},
			// 搜索页面
			tosearchPage() {
			  uni.navigateTo({
			    url: '/pages/goodsDetail/goodsSearch/goodsSearch'
			  })
			},
			toPage(id) {
				uni.navigateTo({
				  url: '/pages/goodsDetail/goodsList/goodsList?id=' + id
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.u-wrap {
		height: calc(100vh);
		/* #ifdef H5 */
		height: calc(100vh - var(--window-top) - 50px);
		/* #endif */
		display: flex;
		flex-direction: column;
	}

	.u-menu-wrap {
		flex: 1;
		display: flex;
		overflow: hidden;
	}

	.u-tab-view {
		width: 170rpx;
		height: 100%;
	}

	.u-tab-item {
		height: 80rpx;
		display: flex;
		align-items: center;
		padding-left: 30rpx;
		font-size: 28rpx;
		color: #444;
		font-weight: 400;
	}
	
	.u-tab-item-active {
		position: relative;
		color: #000;
		font-size: 30rpx;
		font-weight: 600;
		background: #fff;
	}
	
	.u-tab-item-active::before {
		content: "";
		position: absolute;
		border-left: 4px solid $u-main-text-color;
		height: 40rpx;
		left: 10rpx;
		top: 20rpx;
	}

	.u-tab-view {
		height: 100%;
	}
	
	.class-item {
		margin-bottom: 30rpx;
		background-color: #fff;
		padding: 16rpx;
		border-radius: 8rpx;
	}
	
	.item-title {
		font-size: 26rpx;
		color: $u-main-color;
		font-weight: bold;
	}
	
	.item-menu-name {
		font-weight: normal;
		font-size: 24rpx;
		color: $u-main-color;
	}
	
	.item-container {
		display: flex;
		flex-wrap: wrap;
	}
	
	.thumb-box {
		width: 33.333333%;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		margin-top: 20rpx;
	}
	
	.item-menu-image {
		width: 120rpx;
		height: 120rpx;
	}
</style>
